package com.ruyue.csinstall.command.impl;

import com.ruyue.csinstall.command.Command;
import com.ruyue.csinstall.model.CsPro;
import com.ruyue.csinstall.utils.FileTools;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.util.List;

/**
 * @author xiawl
 * @create 2017/7/29 14:41.
 */
public class MergeFiletransferXmlCommand implements Command {

    private static final Log log = LogFactory.getLog(MergeFiletransferXmlCommand.class);
    private String backupConfPath;
    private String confPath;

    public MergeFiletransferXmlCommand(CsPro csPro) {
        this.backupConfPath = String.format("%s/conf", csPro.getBackupPath());
        this.confPath = String.format("%s/comm-server/conf", csPro.getUnZipPath());
    }

    @Override
    public boolean execute() {
        log.info(String.format("开始合并filetransfer-config.xml配置文件[%s] 到 [%s]", backupConfPath, confPath));
        List<File> lf = FileTools.ls(confPath, true);
        for(File f : lf){
            if(("filetransfer-config.xml").equals(f.getName())){
                String relativePath = f.getAbsolutePath().substring(confPath.length());
                FileTools.mergeFiletransferXML(String.format("%s%s", backupConfPath, relativePath), String.format("%s%s", confPath, relativePath));
            }
        }
        log.info("合并filetransfer-config.xml配置文件成功");
        return true;
    }
}